/*
 * $Id: CreateRFSMarshallingEndpoint.java,v 1.5 2012/04/24 01:27:51 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2010 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.createrfs;

import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.createrfs.RFSLogger;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.createrfs.request.CreateRFSRequest;
import com.ge.healthcare.autosc.createrfs.response.CreateRFSResponse;
import com.ge.med.service.eds.commons.DataException;
import com.ge.med.service.eds.commons.ResourceException;
import com.ge.med.service.eds.system.SystemFactory;
import com.ge.med.service.eds.system.vo.AssetInformation;

/**
 * Marshalling endpoint used by Spring to convert XML messages into objects and
 * vice versa.
 * 
 */
public class CreateRFSMarshallingEndpoint extends
		AbstractMarshallingPayloadEndpoint {

	/** Handler for the message. */
	private CreateRFSRequestHandler createRFSHandler = null;

	/** Handler for the CARES message. */
	private CARESRequestHandler caresRequestHandler = null;
	/** Handler for the Siebel message. */
	private SiebelRequestHandler siebelRequestHandler = null;

	public void setSiebelRequestHandler(
			SiebelRequestHandler siebelRequestHandler) {
		this.siebelRequestHandler = siebelRequestHandler;
	}

	public void setCaresRequestHandler(CARESRequestHandler caresRequestHandler) {
		this.caresRequestHandler = caresRequestHandler;
	}

	/**
	 * @see org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint#invokeInternal(java.lang.Object)
	 * 
	 * @param object
	 *            object to be handled - passed in by Spring framework.
	 * @return result object
	 */
	@Override
	protected Object invokeInternal(Object object) {
		String methodName = "invokeInternal";

		RFSLogger.info(this.getClass(), methodName, "Got request object:"
				+ object);

		CreateRFSRequest request = (CreateRFSRequest) object;
		String systemId = request.getSystemid();
		String pole = request.getPole();

		/*
		 * If the system exists in sieble, the create a sieble request. Else
		 * check for poles. If pole is north america, then proceed to create a
		 * Cares request.otherwise if the pole is japan, then Create RFS. IF the
		 * pole is neither japan nor north america, then return a failure
		 * response
		 */
		if (isSiebelCRM(systemId, request)) {
			return siebelRequestHandler.handle(request);
		} else {
			if (pole.contains("nam")) {
				return caresRequestHandler.handle(object);
			} else if (pole.contains("jap")) {
				return createRFSHandler.handle(object);
			} else {
				RFSLogger.info(this.getClass(), methodName, "Unknown pole:"
						+ pole);
				return new CreateRFSResponse(null, null, "FAILED",
						"Not allowed for the pole: " + pole);
			}
		}

		/*
		 * if (pole.contains("jap")) { return createRFSHandler.handle(object); }
		 * else if(pole.contains("nam")){ // check only for NAM. Because there
		 * is AUS and might be in future there are other pole also if
		 * (utilityService.isSiebelCRM(systemId)) { return
		 * siebelRequestHandler.handle(object); } else { return
		 * caresRequestHandler.handle(object); } }else{ return new
		 * CreateRFSResponse(null, null, "FAILED",
		 * "Not allowed for the pole: "+pole); }
		 */

	}

	/**
	 * Setter for the handler. This need to be here for the Spring framework.
	 * 
	 * @param handler
	 *            class that will handle CreateRFS requests.
	 */
	public void setCreateRFSHandler(CreateRFSRequestHandler handler) {
		this.createRFSHandler = handler;
	}

	private boolean isSiebelCRM(String systemId, CreateRFSRequest request) {
		boolean isSiebel = false;

		boolean isSiebelEnabled = ApplicationInfo.getInstance()
				.getBooleanProperty(PropertyConstants.SIEBEL_ENABLED, "false");
		if (isSiebelEnabled) {
			try {
				AssetInformation asset = SystemFactory.getInstance()
						.getAssetObject().getDetails(systemId);
				request.setAssetInfo(asset);
				RFSLogger.info(this.getClass(), "isSiebelCRM", "Asset Info:"
						+ asset);

				if (ASCUtil.containsInProperty(
						PropertyConstants.SIEBEL_CRM_IDS, ",", asset.getCrm())) {
					isSiebel = true;
				}

			} catch (ResourceException e) {
				RFSLogger.error(this.getClass(), "isSiebelCRM",
						"ResourceException caught in isSiebel", e);
			} catch (DataException e) {
				RFSLogger.error(this.getClass(), "isSiebelCRM",
						"DataException caught in isSiebel", e);
			}
		} else {
			RFSLogger.info(this.getClass(), "isSiebelCRM",
					"Siebel checks haven't been enabled yet");
		}
		RFSLogger.info(this.getClass(), "isSiebelCRM", "isSiebel:" + isSiebel);
		return isSiebel;

	}

}
